<?php

namespace app\admin\model;

use think\Cache;
use think\Model;

class Info extends Model
{
    // 表名
    protected $name = 'agent_info';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'province_text',
        'city_text',
        'area_text',
        'clerk_text',
        'docking_text'

    ];
    /**
     * 获取省的名称
     * @return array
     */
    public function getProvinceOrderList()
    {
        if(!$list = \think\Cache::get('arealist'))
        {
            $area = model(GetTableModel('Area'))::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }
    public function getProvinceTextAttr($value, $data)
    {
        $value = $value ? $value : $data['province'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCityTextAttr($value, $data)
    {
        $value = $value ? $value : $data['city'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAreaTextAttr($value, $data)
    {
        $value = $value ? $value : $data['area'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getClerkTextAttr($value, $data){
        $value = $value ? $value : $data['clerk'];
        return TableField('Admin','username',$value,'userid');
    }
    public function getDockingTextAttr($value, $data){
        $value = $value ? $value : $data['docking'];
        return TableField('Admin','username',$value,'userid');
    }
}
